from tkinter import *
from tkinter.ttk import *
from os import system
from tkinter import scrolledtext
import tkinter.messagebox
import ctypes
from importlib import reload
target = 0

P = False
t = 0

send_button_pos = 0
body = ''


def send(s=0):
    global target
    for i in range(s, len(target.data)):
        if P:
            return i
        log['state'] = 'normal'
        log.insert(END, "[第"+str(i+1)+"条] 正在发给："+target.data[i][0]+'\n')
        log['state'] = 'disabled'
        log.see(END)
        log.update()
        text = "【人力资源部】" + target.data[i][0]+"同学："+body
        system(".\platform-tools\\adb shell am start -a android.intent.action.SENDTO -d sms:" +
               target.data[i][1]+" --es sms_body "+text)
        for j in range(send_button_pos):
            system(".\platform-tools\\adb shell input keyevent 22")
        system(".\platform-tools\\adb shell input keyevent 66")
        bar['value'] = (i+1)*100//len(target.data)
        log['state'] = 'normal'
        log.insert(END, "\t已发送！\n")
        log['state'] = 'disabled'
        log.see(END)
        window.update()
    print("All done!")
    tkinter.messagebox.showinfo('提示', '完成！')
    button2['state'] = 'disabled'
    button1['state'] = 'normal'
    combo['state'] = 'normal'
    txt['state'] = 'normal'


window = Tk()
window.title("SMS通知")
sw = window.winfo_screenwidth()
sh = window.winfo_screenheight()
window.geometry("{:.0f}x{:.0f}+{:.0f}+{:.0f}".format(sw *
                                                     0.4, sh*0.4, sw*0.6/2, sh*0.6/2))
window.resizable(width=False, height=False)

label1 = LabelFrame(window, text="通知主体")
label1.pack(fill=X)

txt = scrolledtext.ScrolledText(label1, height=5, wrap=WORD)
txt.pack(anchor=CENTER, fill=Y)


label2 = LabelFrame(window, text="发送键位置")
label2.pack()

combo = Combobox(label2, state='readonly')
combo['values'] = (1, 2)
combo.current(0)
combo.pack()

bar = Progressbar(window, length=0.7*sw * 0.4)
bar['value'] = 0
bar.pack()


def onClick1():
    button1['state'] = 'disabled'
    combo['state'] = 'disabled'
    button2['state'] = 'normal'
    txt['state'] = 'disabled'
    global body, send_button_pos, t
    send_button_pos = int(combo.get())
    body = txt.get('0.0', END)
    system('get-target')
    global target
    target = __import__('target')
    reload(target)
    t = send()


def onClick2():
    global P, t
    if button2['text'] == '暂停':
        button2['text'] = '继续'
        P = True
    else:
        button2['text'] = '暂停'
        P = False
        t = send(t)


button1 = Button(window, text="发送", command=onClick1)
button1.pack()
button2 = Button(window, text="暂停", command=onClick2, state='disabled')
button2.pack()

log = scrolledtext.ScrolledText(window, state='disabled')
log.pack()

window.mainloop()
